//
//  DesignAbleView.m
//  DemoDesignable
//
//  Created by WTFSSD on 2017/7/26.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "DesignAbleImageView.h"

@implementation DesignAbleImageView


-(void)setCornerRadius:(NSString* )cornerRadius{
    _cornerRadius = cornerRadius;
    [self setNeedsLayout];

}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    NSArray * arr = [self.cornerRadius componentsSeparatedByString:@","];
    if (arr.count == 1) {
        CGFloat r = [arr.firstObject floatValue];
        if (r<1) {
            r *= self.frame.size.height;
        }
        self.layer.cornerRadius = r;
    }else if(arr.count>1){
        CGFloat ru = 0.f;
        CGFloat rb = 0.f;
        CGFloat lu = 0.f;
        CGFloat lb = 0.f;
        if (arr.count == 2) {
            CGFloat r1 = [arr[0] floatValue];
            CGFloat r2 = [arr[1] floatValue];
            ru=r1<1?r1*self.frame.size.height:r1;
            rb=r2<1?r2*self.frame.size.height:r2;
        }
        if (arr.count == 3) {
            CGFloat r1 = [arr[0] floatValue];
            CGFloat r2 = [arr[1] floatValue];
            CGFloat r3 = [arr[2] floatValue];
            ru=r1<1?r1*self.frame.size.height:r1;
            rb=r2<1?r2*self.frame.size.height:r2;
            lb=r2<1?r3*self.frame.size.height:r3;
        }
        if (arr.count>3) {
            CGFloat r1 = [arr[0] floatValue];
            CGFloat r2 = [arr[1] floatValue];
            CGFloat r3 = [arr[2] floatValue];
            CGFloat r4 = [arr[3] floatValue];
            ru=r1<1?r1*self.frame.size.height:r1;
            rb=r2<1?r2*self.frame.size.height:r2;
            lb=r3<1?r3*self.frame.size.height:r3;
            lu=r4<1?r4*self.frame.size.height:r4;
        }
        
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        
        UIBezierPath * maskPath = [[UIBezierPath alloc] init];
        
        [maskPath moveToPoint:CGPointMake(self.frame.size.width-ru, 0)];
        [maskPath addArcWithCenter:CGPointMake(self.frame.size.width-ru, ru) radius:ru startAngle:-M_PI/2 endAngle:0 clockwise:YES];
        
        [maskPath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height-rb)];
        [maskPath addArcWithCenter:CGPointMake(self.frame.size.width-rb, self.frame.size.height-rb) radius:rb startAngle:0 endAngle:M_PI/2 clockwise:YES];
        
        [maskPath addLineToPoint:CGPointMake(lb, self.frame.size.height)];
        [maskPath addArcWithCenter:CGPointMake(lb, self.frame.size.height-lb) radius:lb startAngle:M_PI/2 endAngle:M_PI clockwise:YES];
        
        [maskPath addLineToPoint:CGPointMake(0, self.frame.size.height-lu)];
        [maskPath addArcWithCenter:CGPointMake(lu, lu) radius:lu startAngle:-M_PI endAngle: -M_PI/2 clockwise:YES];
        
        [maskPath addLineToPoint:CGPointMake(self.frame.size.width-ru, 0)];
        [maskPath fill];
        maskLayer.path = maskPath.CGPath;
        
        self.layer.mask = maskLayer;
    }
    
}

@end
